<template>
  <el-dialog
    :title="!dataForm.cellId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="updvisible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="小区名称" prop="cellName">
      <el-input v-model="dataForm.cellName" placeholder="小区名称"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="地区id(省份id)" prop="shenId">
      <el-input v-model="dataForm.shenId" placeholder="地区id(省份id)"></el-input>
    </el-form-item>
    <el-form-item label="地区id(市id)" prop="shiId">
      <el-input v-model="dataForm.shiId" placeholder="地区id(市id)"></el-input>
    </el-form-item>
    <el-form-item label="地区id(县id)" prop="xianId">
      <el-input v-model="dataForm.xianId" placeholder="地区id(县id)"></el-input>
    </el-form-item>
    <el-form-item label="所属区域    如：海南-海口-龙华区" prop="address">
      <el-input v-model="dataForm.address" placeholder="所属区域    如：海南-海口-龙华区"></el-input>
    </el-form-item>
    <el-form-item label="状态  0-正常 1-禁用" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态  0-正常 1-禁用"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
      <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="dimension">
      <el-input v-model="dataForm.dimension" placeholder="纬度"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="updvisible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        updvisible: false,
        dataForm: {
          cellId: 0,
          cellName: '',
          updateTime: '',
          shenId: '',
          shiId: '',
          xianId: '',
          address: '',
          status: '',
          longitude: '',
          dimension: ''
        },
        dataRule: {
          cellName: [
            { required: true, message: '小区名称不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          shenId: [
            { required: true, message: '地区id(省份id)不能为空', trigger: 'blur' }
          ],
          shiId: [
            { required: true, message: '地区id(市id)不能为空', trigger: 'blur' }
          ],
          xianId: [
            { required: true, message: '地区id(县id)不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '所属区域    如：海南-海口-龙华区不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态  0-正常 1-禁用不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          dimension: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.cellId = id || 0
        this.updvisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.cellId) {
            this.$http({
              url: this.$http.adornUrl(`/house/cell/info/${this.dataForm.cellId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.cellName = data.cell.cellName
                this.dataForm.updateTime = data.cell.updateTime
                this.dataForm.shenId = data.cell.shenId
                this.dataForm.shiId = data.cell.shiId
                this.dataForm.xianId = data.cell.xianId
                this.dataForm.address = data.cell.address
                this.dataForm.status = data.cell.status
                this.dataForm.longitude = data.cell.longitude
                this.dataForm.dimension = data.cell.dimension
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/cell/${!this.dataForm.cellId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'cellId': this.dataForm.cellId || undefined,
                'cellName': this.dataForm.cellName,
                'updateTime': this.dataForm.updateTime,
                'shenId': this.dataForm.shenId,
                'shiId': this.dataForm.shiId,
                'xianId': this.dataForm.xianId,
                'address': this.dataForm.address,
                'status': this.dataForm.status,
                'longitude': this.dataForm.longitude,
                'dimension': this.dataForm.dimension
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.viupdvisiblesible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
